package com.xxx.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.car.entity.*;
import com.xxx.car.entity.vo.request.ReqInvoice;
import com.xxx.car.entity.vo.request.ReqInvoiceAdminQuery;
import com.xxx.car.entity.vo.request.ReqInvoiceQuery;
import com.xxx.car.entity.vo.request.ReqInvoiceUpdate;
import com.xxx.car.entity.vo.response.RespInvoice;
import com.xxx.car.entity.vo.response.RespInvoiceDetail;
import com.xxx.car.mapper.ITbMapper;
import com.xxx.car.mapper.TbInvoiceMapper;
import com.xxx.car.mapper.TbTravelOrderMapper;
import com.xxx.car.mapper.TbUserMapper;
import com.xxx.car.service.InvoiceService;
import com.xxx.car.service.UserService;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by youxiaojia on 2017/6/22.
 */
@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    TbInvoiceMapper tbInvoiceMapper;
    @Autowired
    TbTravelOrderMapper tbTravelOrderMapper;
    @Autowired
    UserService userService;
    @Autowired
    TbUserMapper tbUserMapper;
    @Autowired
    ITbMapper iTbMapper;

    @Override
    public List<TbInvoice> queryInvoiceList(Integer userId) {
        TbInvoiceExample tbInvoiceExample = new TbInvoiceExample();
        TbInvoiceExample.Criteria criteria = tbInvoiceExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        tbInvoiceExample.setOrderByClause(" create_time desc ");
        List<TbInvoice> tbInvoiceList = tbInvoiceMapper.selectByExample(tbInvoiceExample);
        return tbInvoiceList;
    }

    @Override
    public APIResponse applyInvoice(ReqInvoice reqInvoice) {
        TbInvoice tbInvoice = new TbInvoice();
        BeanUtils.copyProperties(reqInvoice,tbInvoice);
        tbInvoice.setStatus((byte)0);
        int result = tbInvoiceMapper.insertSelective(tbInvoice);
        if (result > 0){
            return APIResponse.returnSuccess("申请发票成功");
        }
        return APIResponse.returnFail("申请发票失败");
    }

    @Override
    public RespInvoiceDetail queryInvoiceDetail(ReqInvoiceQuery reqInvoiceQuery) {
        RespInvoiceDetail respInvoiceDetail = new RespInvoiceDetail();
        TbInvoice tbInvoice = tbInvoiceMapper.selectByPrimaryKey(reqInvoiceQuery.getId());
        if (tbInvoice == null) {
            return null;
        }
        BeanUtils.copyProperties(tbInvoice,respInvoiceDetail);

        String[] travelOrderIds = org.apache.commons.lang3.StringUtils.split(tbInvoice.getTravelIds(), ",");
        List<Integer> list = new ArrayList<>();
        for (String travelOrderId : travelOrderIds) {
            list.add(Integer.parseInt(travelOrderId));
        }
        TbTravelOrderExample tbTravelOrderExample = new TbTravelOrderExample();
        TbTravelOrderExample.Criteria criteria = tbTravelOrderExample.createCriteria();
        criteria.andIdIn(list);
        List<TbTravelOrder> tbTravelOrderList = tbTravelOrderMapper.selectByExample(tbTravelOrderExample);
        respInvoiceDetail.setTravelOrderList(tbTravelOrderList);

        TbUser tbUser = tbUserMapper.selectByPrimaryKey(tbInvoice.getUserId());
        respInvoiceDetail.setMobile(tbUser.getMobile());
        return respInvoiceDetail;
    }

    @Override
    public APIResponse updateInvoice(ReqInvoice reqInvoice) {
        TbInvoice tbInvoice = new TbInvoice();
        BeanUtils.copyProperties(reqInvoice,tbInvoice);

        TbInvoiceExample tbInvoiceExample = new TbInvoiceExample();
        TbInvoiceExample.Criteria criteria = tbInvoiceExample.createCriteria();
        criteria.andStatusGreaterThan((byte)0);
        int result = tbInvoiceMapper.updateByExampleSelective(tbInvoice,tbInvoiceExample);
        if (result != 1) {
            return APIResponse.returnFail("发票已被受理,不能修改");
        }
        return APIResponse.returnSuccess();
    }

    @Override
    public PageInfo queryInvoiceAdminList(ReqInvoiceAdminQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<RespInvoice> respInvoiceList = iTbMapper.queryInvoiceAdminList(query);
        return new PageInfo<>(respInvoiceList);
    }

    @Override
    public int countCompleteInvoice() {
        TbInvoiceExample tbInvoiceExample = new TbInvoiceExample();
        TbInvoiceExample.Criteria criteria = tbInvoiceExample.createCriteria();
        criteria.andStatusEqualTo((byte)1);
        int result = tbInvoiceMapper.countByExample(tbInvoiceExample);
        return result;
    }

    @Override
    public APIResponse updateInvoiceReply(ReqInvoiceUpdate update) {
        TbInvoice tbInvoice = new TbInvoice();
        BeanUtils.copyProperties(update,tbInvoice);
        if (update.getIsAgree()==0){
            tbInvoice.setStatus((byte)2);
        }
        if (update.getIsAgree()==1){
            tbInvoice.setStatus((byte)1);
        }

        TbInvoiceExample tbInvoiceExample = new TbInvoiceExample();
        TbInvoiceExample.Criteria criteria = tbInvoiceExample.createCriteria();
        criteria.andStatusEqualTo((byte)0);
        criteria.andIdEqualTo(update.getInvoiceId());
        int result = tbInvoiceMapper.updateByExampleSelective(tbInvoice,tbInvoiceExample);
        if (result != 1) {
            return APIResponse.returnFail("发票已被受理,不能修改");
        }
        return APIResponse.returnSuccess();
    }
}
